package com.zw.pdm.module.system.service;

import com.zw.pdm.entity.sys.Log;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.system.mapper.LogMapper;
import com.zw.pdm.module.system.repository.LogRepository;
import com.zw.pdm.util.BeanUtil;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/11/28 09:11
 */
@Service
@SuppressWarnings("all")
public class LogService {

    @Autowired
    private LogMapper logMapper;
    @Autowired
    private LogRepository logRepository;


    /**
     * 查询日志
     *
     * @param log 条件
     * @return list
     */
    public List<Log> findByCondition(Log log) {
        return logMapper.findByCondition(log);
    }

    /**
     * 保存日志
     *
     * @param log 日志类
     * @return log
     */
    public Log saveLog(Log log) {
        return logRepository.save(log);
    }

    /**
     * 保存日志
     *
     * @param title 操作名
     * @param content 操作内容
     * @return log
     */
    public Log saveLog(String title, String content) {
        User user = ShiroUtils.getUser();
        Log log = new Log();
        log.setAccount(user.getAccount());
        log.setOperator(user.getName());
        log.setTitle(title);
        log.setOperTime(new Date());
        log.setContent(content);
        log.setAddress(ShiroUtils.getIp());
        return saveLog(log);
    }

    /**
     * 保存日志
     *
     * @param title 操作名
     * @param before 源对象
     * @param after 目标对象
     * @return log
     */
    public Log saveLog(String title, Object before, Object after) {
        return saveLog(title, BeanUtil.compare(before, after));
    }

}
